<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Areas extends Model
{
    use HasFactory;

    protected $table = 'sp_areas';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //查询地区信息
    protected function GetAreas($where)
    {
        $data = $this
            ->where($where)
            ->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //根据name查询地区信息
    protected function GetAreasByName($where)
    {
        $data = $this
            ->where($where)
            ->first();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //根据id查询地区信息
    protected function GetAreasById($id)
    {
        $data = $this->where('id',$id)->select('name')->first();
        $data = $data ? $data->toArray() : array();
        if(!empty($data)){
            return $data['name'];
        }else{
            return '';
        }

    }

    //获取全部城市
    protected function GetAreasList(){
        $data = [];
        $items = $this->where([['level','<',3]])->select('id','pid','name')->get();
        $items = $items ? $items->toArray() : array();
        if(!empty($items)){
            $generateTree = function ($items, $parentKey = 'pid') {
                $tree = $temp = [];
                foreach ($items as $item) {
                    $temp[$item['id']] = $item;
                }
                foreach ($items as $item) {
                    if (isset($temp[$item[$parentKey]])) {
                        $temp[$item[$parentKey]]['sub'][] = &$temp[$item['id']];
                    } else {
                        $tree[] = &$temp[$item['id']];
                    }
                    // 下面可以去掉 pid 元素
                    unset($temp[$item['id']][$parentKey]);
                }
                return $tree;
            };
            $data = $generateTree($items);
        }
        return $data;
    }

    //获取所有区域
    protected function GetAllAreasList(){
        $data = $this->select('id','pid','name')->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    /**
     * 区域数据处理
     * @param $items
     * @return array
     */
    public static function handle($items){
        $data = [];
        if(!empty($items)){
            $generateTree = function ($items, $parentKey = 'pid') {
                $tree = $temp = [];
                foreach ($items as $item) {
                    $temp[$item['id']] = $item;
                }
                foreach ($items as $item) {
                    if (isset($temp[$item[$parentKey]])) {
                        $temp[$item[$parentKey]]['sub'][] = &$temp[$item['id']];
                    } else {
                        $tree[] = &$temp[$item['id']];
                    }
                    // 下面可以去掉 pid 元素
                    unset($temp[$item['id']][$parentKey]);
                }
                return $tree;
            };
            $data = $generateTree($items);
        }
        return $data;
    }


}
